using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace MONA.Modules.Marketing.BLL
{
    public partial class Action
    {
        #region Private members.

        protected int _ID;
        protected string _Name = string.Empty;
        protected string _Description = string.Empty;
        protected string _Function = string.Empty;
        protected int _ChildActionID;
        protected int _NextActionID;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public string Name
        {
            set { this._Name = value; }
            get { return this._Name; }
        }

        public string Description
        {
            set { this._Description = value; }
            get { return this._Description; }
        }

        public string Function
        {
            set { this._Function = value; }
            get { return this._Function; }
        }

        public int ChildActionID
        {
            set { this._ChildActionID = value; }
            get { return this._ChildActionID; }
        }

        public int NextActionID
        {
            set { this._NextActionID = value; }
            get { return this._NextActionID; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Action Load(int iD)
        {
            string spName = "[dbo].p_MARKETING_Action_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Action entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Action();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
                if (!reader.IsDBNull(reader.GetOrdinal("Function"))) entity.Function = reader.GetString(reader.GetOrdinal("Function"));
                if (!reader.IsDBNull(reader.GetOrdinal("ChildActionID"))) entity.ChildActionID = reader.GetInt32(reader.GetOrdinal("ChildActionID"));
                if (!reader.IsDBNull(reader.GetOrdinal("NextActionID"))) entity.NextActionID = reader.GetInt32(reader.GetOrdinal("NextActionID"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------


        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_MARKETING_Action_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_MARKETING_Action_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Action_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Action_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(string name, string description, string function, int childActionID, int nextActionID)
        {
            Action entity = new Action();
            entity.Name = name;
            entity.Description = description;
            entity.Function = function;
            entity.ChildActionID = childActionID;
            entity.NextActionID = nextActionID;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Action_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@Function", SqlDbType.NVarChar, this._Function);
            db.AddInParameter(dbCommand, "@ChildActionID", SqlDbType.Int, this._ChildActionID);
            db.AddInParameter(dbCommand, "@NextActionID", SqlDbType.Int, this._NextActionID);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, string name, string description, string function, int childActionID, int nextActionID)
        {
            Action entity = new Action();
            entity.ID = iD;
            entity.Name = name;
            entity.Description = description;
            entity.Function = function;
            entity.ChildActionID = childActionID;
            entity.NextActionID = nextActionID;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Action_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, this._Description);
            db.AddInParameter(dbCommand, "@Function", SqlDbType.NVarChar, this._Function);
            db.AddInParameter(dbCommand, "@ChildActionID", SqlDbType.Int, this._ChildActionID);
            db.AddInParameter(dbCommand, "@NextActionID", SqlDbType.Int, this._NextActionID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Action entity = new Action();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Action_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}